#!/bin/bash

echo "﹎﹍﹎﹍﹎﹍﹎﹍﹎﹍﹎﹍﹎﹍﹎﹍﹎"
echo "对Jar包安装东方通WebServer..."
echo "要求必须有 unip , jar 命令!"
echo "要求在本文件夹下,必须有一个Jar文件(或指定一个Jar文件),完成后生成到 out 文件夹下."
set -eo pipefail

# 当前位置是 "项目文件夹/web-server"
ori_path=$(pwd)
project_path=$(cd `dirname $0`; pwd)
echo $project_path
cd $project_path

jar_name=$1

if [ -n "$jar_name"]; then
  jar_name=$(ls -1 -F -r *.jar | head -1)
fi

if [ "$jar_name" == "" ]; then
  echo "找不到Jar包,请指定,或把Jar包放到当前目录下! 退出!"
  exit 1
fi


echo "要处理的Jar包: $jar_name"


function process(){
  target="$1"


  fc=$(ls $target/tomcat-embed-core-*.jar -l | grep "^-"| wc -l)
  if [ $fc -eq 0 ]; then
      echo "找不到 $target/tomcat-embed-core-* 包, $fc！退出"
      return
  fi

  rm -f $target/tomcat-embed-core-*
  rm -f $target/tomcat-embed-el-*

  cp -f web-server/tongweb-embed-*.jar  $target
  cp -f web-server/tongweb-spring-boot-starter-2.x-*.jar  $target
#  mv -n web-server/tongweb-jsp-*.jar  $target

  fc=$(ls $target/tomcat-embed-websocket-*.jar -l |grep "^-"| wc -l)
  if [ $fc -gt 0 ]; then
      rm -f $target/tomcat-embed-websocket-*.jar
    cp -f web-server/tongweb-spring-boot-websocket-2.x-*.jar  $target
  fi

  echo "完成Jar包替换， ls $target/tongweb*"
  ls $target/tongweb*
}

rm -rf jar-unzip && mkdir -p jar-unzip

rm -rf out && mkdir -p out

echo "unzip -q $jar_name -d jar-unzip"
unzip -q $jar_name -d jar-unzip

process jar-unzip/BOOT-INF/lib


cd jar-unzip


echo "jar cf0M  ../$jar_name *"
jar cf0M  ../out/$jar_name *

cd $ori_path
echo "完成东方通WebServer"
echo "﹊﹉﹊﹉﹊﹉﹊﹉﹊﹉﹊﹉﹊﹉﹊﹉﹊"